package com.wzh.musichome.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzh.musichome.common.ConstantCode;
import com.wzh.musichome.common.ResponseTemplate;
import com.wzh.musichome.entity.dto.AdminLoginDTO;
import com.wzh.musichome.entity.pojo.Admin;
import com.wzh.musichome.mapper.AdminMapper;
import com.wzh.musichome.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 管理员 服务实现类
 * </p>
 *
 * @author wzh
 * @since 2020-12-10
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Object login(AdminLoginDTO adminLoginDTO, HttpSession session) {
        String name = adminLoginDTO.getName();
        String password = adminLoginDTO.getPassword();
        //检测用户名和密码是否为空
        if (StringUtils.isBlank(name) || StringUtils.isBlank(password)) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_USERNAME_PASSWORD_EMPTY, "用户名/密码不能为空");
        }
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        Admin admin = adminMapper.selectOne(wrapper);
        if (admin == null){
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_USER_NULL, "用户不存在");
        }
        if (!password.matches(admin.getPassword())){
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_PASSWORD_WRONG, "密码错误");
        }
        ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
        data.setMsg("登录成功");
        session.setAttribute("name",name);
        return data ;
    }
}
